package com.example.myviewmodelframe.base.model

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.example.myviewmodelframe.base.event.BaseActionEvent

/**
 * @author tgw
 * @date 2021/6/10
 * @describe
 */
open class BaseViewModel : ViewModel, IBaseModelAction {

    var actionLiveDataP: MutableLiveData<BaseActionEvent>? = null

    private var baseActionEvent: BaseActionEvent? = null
    lateinit var lifecycleOwner: LifecycleOwner

    constructor() {
        actionLiveDataP = MutableLiveData<BaseActionEvent>()
    }

    override fun startLoading() {
        startLoading(null)
    }

    override fun startLoading(msg: String?) {
        baseActionEvent = BaseActionEvent(BaseActionEvent.SHOW_LOADING_DIALOG)
        baseActionEvent?.run {
            message = msg
        }
        actionLiveDataP?.value = baseActionEvent
    }

    override fun dismissLoading() {
        baseActionEvent = BaseActionEvent(BaseActionEvent.DISMISS_LOADING_DIALOG)
        baseActionEvent?.run {
            message = "1首次隐藏"
        }
        actionLiveDataP?.postValue(baseActionEvent)
//       postValue 只会执行最后的事件，postValue事件丢失 --》https://www.jianshu.com/p/b2646bf112ce
//        baseActionEvent = BaseActionEvent(BaseActionEvent.DISMISS_LOADING_DIALOG)
//
//        baseActionEvent?.run {
//            message = "2首次隐藏"
//        }
//        actionLiveDataP?.postValue(baseActionEvent)
//        baseActionEvent?.run {
//            message = "3首次隐藏"
//        }
//        actionLiveDataP?.postValue(baseActionEvent)

//        actionLiveDataP.lifecycleScope.launch {
//            val params = TextViewCompat.getTextMetricsParams(textView)
//            val precomputedText = withContext(Dispatchers.Default) {
//                PrecomputedTextCompat.create(longTextContent, params)
//            }
//            TextViewCompat.setPrecomputedText(textView, precomputedText)
//        }
    }

    override fun showToast() {
        baseActionEvent = BaseActionEvent(BaseActionEvent.SHOW_TOAST)
        actionLiveDataP?.value = baseActionEvent
    }

    override fun getActionLiveData(): MutableLiveData<BaseActionEvent>? {
        return actionLiveDataP
    }


}